﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class 多线程_多线程Collect : System.Web.UI.Page
{

    string Data = "";

    /// <summary>
    /// 数据操作通用方法
    /// </summary>
    Tong.XmlHttp.GetWebData GetData = new Tong.XmlHttp.GetWebData();
    protected void Page_Load(object sender, EventArgs e)
    {
        //test how to use Array


    }

    public void  UseMoreThareds(object url)
    {
        string urlx = (string)url;
        Data += XmlParser("<data>" + Tong.WorkXml.HtmlToXML.HTMLConvert(GetData.GetWebDate(urlx, "GB2312")) + "</data>") + "<br/>111111111111111111111111<br/>";
        CurrTharedsstate = true;
    }

    public volatile bool CurrTharedsstate = false;

    //执行
    protected void Button1_Click(object sender, EventArgs e)
    {



        #region 点击 事件,用于搜索内容



      // Response.Write(Tong.WorkXml.HtmlToXML.XmlConvert(FormValidation1.Text));
      // FormValidation1.Text = Tong.WorkXml.HtmlToXML.XmlConvert(FormValidation1.Text);
      // FormValidation1.Text = Tong.WorkXml.HtmlToXML.HTMLConvert(.Text);
      // Response.Write("<data>" + FormValidation1.Text + "</data>");
      // Response.Charset = "Utf-8";
      // Response.ContentType = "text/xml";
      //start ParSer XML file Contont



      //开始分析MXL文件内容
      //Response.Write(XmlParser("<data>" + FormValidation1.Text + "</data>")); ;


      //cycle parser get urls
      string[] urls = TextBox1.Text.Replace("\n", "").Split('\r');
      Response.Write(urls.Length);

      DateTime d = DateTime.Now;
      //使用单线程
      int j = 0;
      while (j < urls.Length)
      {
          if (urls[j].Trim() != "")
          {
              Data += XmlParser("<data>" + Tong.WorkXml.HtmlToXML.HTMLConvert(GetData.GetWebDate(urls[j], "GB2312")) + "</data>") + "<br/>111111111111111111111111<br/>";
          }
          j++;
      }
      Data += "<br/>所用时间:" + (DateTime.Now - d);
      Tong.WorkXml.OperateXmlByDataSet.WriteXml(Data, Server.MapPath("c.htm"));
      //使用多线程
      Data = "";
      d = DateTime.Now;
      j = 0;
      while (j < urls.Length)
      {
          if (urls[j].Trim() != "")
          {
              System.Threading.Thread Thr = new System.Threading.Thread(UseMoreThareds);
              Thr.Start(urls[j]);

              j++;

              System.Threading.Thread Thr1 = new System.Threading.Thread(UseMoreThareds);
              Thr1.Start(urls[j]);


              j++;
              System.Threading.Thread Thr2 = new System.Threading.Thread(UseMoreThareds);
              Thr2.Start(urls[j]);

              Thr.Join();
              Thr1.Join();
              Thr2.Join();
          }
          j++;
      }
      Data += "<br/>所用时间:" + (DateTime.Now - d) + "";
      Tong.WorkXml.OperateXmlByDataSet.WriteXml(Data, Server.MapPath("d.htm"));
      Response.End();


        #endregion


    }


    /// <summary>
    /// 分析XML文件
    /// 
    /// </summary>
    /// <param name="xmlString"></param>
    /// <returns></returns>
    public string XmlParser(string xmlString)
    {
        //[\u4e00-\u9fa5\d\w１２３４５６７８９～！·＃￥％……—＊（）——＋／”》“‘’；。、？，：…《]+[\u4e00-\u9fa5１２３４５６７８９～！·＃￥％……—＊（）——＋／”》“‘’；。、？，：…《]
        //More Array Value
        string mainContont = "";//文章内容
        XmlDataDocument MyXml = new XmlDataDocument();

        try
        {
        MyXml.LoadXml(xmlString);
        mainContont = MyXml.DocumentElement.GetElementsByTagName("body")[0].OuterXml;
        //如果不能得到body内容   
        if (mainContont.Length <= 100)
        {
            mainContont = xmlString;
        }
        System.Text.RegularExpressions.MatchCollection MC = System.Text.RegularExpressions.Regex.Matches(mainContont, @"[\u4e00-\u9fa5\d\w１２３４５６７８９～！!·＃￥％……—＊（）——＋／”》“‘’，；。、？，：…《]+[\u4e00-\u9fa5１２３４５６７８９～！!·＃￥％……—＊（!）——＋／”》“‘，’\r\n；。、？，：…《]");



        int j = 0;
        //创建 一个数组用于保存 从第一个开始的特定数据到下一个特定数据出面的距离
        System.Collections.Generic.List<AppOvt> ppu = new System.Collections.Generic.List<AppOvt>();//初始化对像
        System.Collections.Generic.List<AppOvt> ppu_Old = new System.Collections.Generic.List<AppOvt>();//初始化对像用于保存没有排序的数据

        int olk = 0; //保存的距离数据
        int Isone = 0;//如果是第一个
        //冒泡排序所有到的参数
        int tempint = 0;//currpant line
        int tempspace = 0;//space 
        string tempcont = "";//初始化内容
        //最小距离值
        int MinSpaceValue = 11;//初始化为放弃的值

        //表示 找到的中文字符  50 个以上就算一个有效的值
        int MaxChinaeseStrValue = 50;

        while (j < MC.Count)
        {
            //Response.Write(MC[j].Value+" "+ ((MC[j].Value.Length >= 50) ? "1" : "0")+"<br/>");
            //从开始第一个1 到下一个1 中间格的数据
            if (MC[j].Value.Length >= MaxChinaeseStrValue)//20个不对
            {
                if (Isone == 0)
                {
                    Isone = j;//如果是第一个数据
                    olk = j;

                    //当前位置,上一个的 space
                    AppOvt oou = new AppOvt();
                    oou.currrow = j;
                    oou.space = Isone;
                    oou.cont = MC[j].Value;
                    ppu.Add(oou);


                    //初始化原始
                    AppOvt ccc = new AppOvt();
                    ccc.currrow = j;
                    ccc.space = olk;
                    ccc.cont = MC[j].Value;
                    ppu_Old.Add(ccc);

                }
                else//如果不是第一个的话
                {
                    olk = j - olk;
                    AppOvt oou = new AppOvt();
                    oou.currrow = j;
                    oou.space = olk;
                    oou.cont = MC[j].Value;
                    ppu.Add(oou);

                    //初始化原始
                    AppOvt ccc = new AppOvt();
                    ccc.currrow = j;
                    ccc.space = olk;
                    ccc.cont = MC[j].Value;
                    ppu_Old.Add(ccc);

                    olk = j;
                }
            }
            j++;
        }


        //xml line count
        //得到所有汉字的东东
        //如果汉字多则可能性大

        //用冒泡把序排下 方便下面得到数据
        for (int ii = 0; ii < ppu.Count; ii++)
        {

            for (int jj = 0; jj < ppu.Count - 1; jj++)
            {
                //如果当前比下一个的数大
                //desc for ppu
                if (ppu[jj].space > ppu[jj + 1].space && ppu[jj].space != 0)
                {
                    tempspace = ppu[jj].space;
                    tempcont = ppu[jj].cont;
                    tempint = ppu[jj].currrow;

                    ppu[jj].space = ppu[jj + 1].space;
                    ppu[jj].currrow = ppu[jj + 1].currrow;
                    ppu[jj].cont = ppu[jj + 1].cont;

                    ppu[jj + 1].space = tempspace;
                    ppu[jj + 1].currrow = tempint;
                    ppu[jj + 1].cont = tempcont;
                }

            }
        }



        //foreach (AppOvt oo in ppu)
        //{
        //    Response.Write("当前数" + oo.currrow);
        //    Response.Write("和上一个的space" + oo.space + "<br/>");
        //}

        //动态创建正则表达式
        string Regexstr = "";
        //得到最小的 距离值 当最小的距离值大于 在2以内的时候动态创建 正则表达式
        //如是在2以外的话，最小距离值 在10以内就动态创建正则表达式 在10以外就放弃
        //并且，第二个最小值不能和第一个最小值的 距离 超过10
        //the low code get minvalue for space
        if (ppu.Count > 1)
        {
            //如果上一行到下一行的值为0的话那么 这一行为第一持程序要移除
            if (ppu_Old[0].space == 0)
            {
                ppu_Old.RemoveAt(0);
                ppu.RemoveAt(0);
            }

            MinSpaceValue = ppu[0].space;//get min space value


            j = 0;
            if (MinSpaceValue <= 10 && ppu[1].space - MinSpaceValue < 10)//start action Regex string
            {
                //动态创建 正则表达式 提取 第一个 距离 的最小值，和第二个 距离 的最小值
                for (int ispace = 0; ispace < ppu_Old.Count; ispace++)
                {

                    if ((ppu_Old[1].space - MinSpaceValue) == 0)
                    {
                        if (ppu_Old[ispace].space <= 1 && j < 4)//只提取两级
                        {
                            Regexstr += ppu_Old[ispace].cont + @"[\w\W]+";
                            j++;
                        }
                    }
                    else
                    {
                        if (ppu_Old[ispace].space <= (ppu_Old[1].space - MinSpaceValue) && j < 4)//只提取两级
                        {
                            Regexstr += ppu_Old[ispace].cont + @"[\w\W]+";
                            j++;
                        }
                    }

                    if (j >= 3)
                    {
                        break;
                    }
                }

            }
        }

        try
        {
            Regexstr = Regexstr.Substring(0, Regexstr.Length - 7);
        }
        catch
        {

        }
        //Response.Write(Regexstr);

        string DocCont = "";//文档内容
        //开始得到内容
        //td,th,div,p,li,ul


        XmlNodeList Td = MyXml.DocumentElement.GetElementsByTagName("Td");//用Td
        XmlNodeList TD = MyXml.DocumentElement.GetElementsByTagName("TD");//用TD
        XmlNodeList tD = MyXml.DocumentElement.GetElementsByTagName("tD");//用tD
        XmlNodeList td = MyXml.DocumentElement.GetElementsByTagName("td");//用td


        XmlNodeList div = MyXml.DocumentElement.GetElementsByTagName("div");//用div
        XmlNodeList Div = MyXml.DocumentElement.GetElementsByTagName("Div");//
        XmlNodeList dIv = MyXml.DocumentElement.GetElementsByTagName("dIv");//
        XmlNodeList diV = MyXml.DocumentElement.GetElementsByTagName("diV");//
        XmlNodeList DIv = MyXml.DocumentElement.GetElementsByTagName("DIv");//
        XmlNodeList dIV = MyXml.DocumentElement.GetElementsByTagName("dIV");//用 div
        XmlNodeList DIV = MyXml.DocumentElement.GetElementsByTagName("DIV");//用 DIV




        //XmlNodeList Th = MyXml.DocumentElement.GetElementsByTagName("th");

        XmlNodeList P = MyXml.DocumentElement.GetElementsByTagName("p");
        XmlNodeList li = MyXml.DocumentElement.GetElementsByTagName("li");
        //XmlNodeList ul = MyXml.DocumentElement.GetElementsByTagName("ul");
        //Response.Write(DocCountTemp);

        #region 得到 td 的数据 XML 区分大小写的
        try
        {
            for (int i = 0; i < Td.Count; i++)
            {
                if (System.Text.RegularExpressions.Regex.Match(Td[i].OuterXml, Regexstr).Value.Length > 0)
                {
                    DocCont = Td[i].OuterXml;
                    // break;
                }
            }
        }
        catch { }

        //使用TD 
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < TD.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(TD[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = TD[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        //使用 tD
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < tD.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(tD[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = tD[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }



        //使用 td
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < td.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(td[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = td[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        #endregion

        #region 得到 div 的数据 XML 区分大小写的
        //使用 div
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < div.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(div[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = div[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        // Div

        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < Div.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(Div[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = Div[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        // dIv
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < dIv.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(dIv[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = dIv[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        // diV
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < diV.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(diV[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = diV[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        // DIv
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < DIv.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(DIv[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = DIv[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }  
        
        // DIv
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < dIV.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(dIV[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = dIV[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }

        //DIV
        try
        {
            if (DocCont == "")
            {

                for (int i = 0; i < DIV.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(DIV[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = DIV[i].OuterXml;
                    }
                }
            }
        }
        catch
        {

        }  
        #endregion







        return DocCont;
        }
        catch
        {
            return "can not read file";
        }
   }


}
/// <summary>
/// 用于保存数据
/// </summary>
public class AppOvt
{

    public int currrow
    {

        get;
        set;
    }

    public int space { get; set; }

    public string cont { get; set; }
}
